﻿package com.tudou.home.newskin.containers
{
    import com.tudou.home.common.button.*;
    import com.tudou.home.common.events.*;
    import com.tudou.home.newskin.buttons.*;
    import com.tudou.home.newskin.consts.*;
    import com.tudou.home.newskin.events.*;
    import com.tudou.home.newskin.model.*;
    import com.tudou.skin.*;
    import com.tudou.track.*;
    import com.tudou.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;

    public class RelativeVideoSprite extends Sprite implements IRelativeVideo, ITransform, ITransFinished
    {
        private var prevNext:AutoPrevNextButton;
        private var videoXNum:int = 4;
        private var relativeList:Array;
        private var yBetween:Number = 0;
        private var showTitle:TextField;
        private var videoYNum:int = 1;
        private var xBetween:Number = 0;
        private var totalPage:int = 0;
        private var _showTransFinished:Boolean = false;

        public function RelativeVideoSprite()
        {
            showTitle = TextFieldUtil.getTextField();
            showTitle.defaultTextFormat = new TextFormat("宋体", 14, 10066329, true);
            showTitle.text = "推荐视频";
            addChild(showTitle);
            prevNext = new AutoPrevNextButton(new PrevPageBTN(), new PrevPageDisableMC(), new NextPageBTN(), new NextPageDisableMC());
            prevNext.setDelay(NewSkinConsts.AUTO_PAGE_TIME);
            addChild(prevNext);
            prevNext.addEventListener(PrevNextEvent.PREVIOUS, prevNextHlr);
            prevNext.addEventListener(PrevNextEvent.NEXT, prevNextHlr);
            return;
        }// end function

        public function set showTransFinished(param1:Boolean) : void
        {
            _showTransFinished = param1;
            return;
        }// end function

        private function relativeHlr(event:MouseEvent) : void
        {
            var _loc_2:* = relativeList.indexOf(event.currentTarget);
            dispatchEvent(new SkinEvent(SkinEvent.BUTTON_LOG, SkinButtonCode.RELATIVE + "|" + _loc_2));
            return;
        }// end function

        public function setPlaylistSingleContinue(param1:Boolean) : void
        {
            return;
        }// end function

        private function prevNextHlr(event:PrevNextEvent) : void
        {
            var _loc_2:* = event.type == PrevNextEvent.PREVIOUS ? ((event.index - 1)) : ((event.index + 1));
            if (_loc_2 >= prevNext.getLength())
            {
                _loc_2 = 0;
            }
            prevNext.setIndex(_loc_2);
            goPage(_loc_2);
            return;
        }// end function

        public function setRelativeList(param1:Array) : void
        {
            var _loc_2:IBaseVideoInfo = null;
            var _loc_3:RelativeVideo = null;
            if (param1 == null || param1.length == 0)
            {
                return;
            }
            relativeList = [];
            var _loc_4:int = 0;
            while (_loc_4 < param1.length)
            {
                
                _loc_2 = param1[_loc_4] as IBaseVideoInfo;
                _loc_3 = new RelativeVideo(_loc_2, _loc_4, _showTransFinished);
                relativeList[_loc_4] = _loc_3;
                addChild(_loc_3);
                _loc_3.addEventListener(MouseEvent.CLICK, relativeHlr);
                _loc_4++;
            }
            return;
        }// end function

        public function setRelativeAreaPerPage(param1:uint, param2:uint) : void
        {
            videoXNum = param1;
            videoYNum = param2;
            return;
        }// end function

        private function goPage(param1:int) : void
        {
            var _loc_3:RelativeVideo = null;
            var _loc_2:int = 0;
            var _loc_4:* = videoXNum * videoYNum;
            var _loc_5:int = 0;
            while (_loc_5 < relativeList.length)
            {
                
                _loc_2 = _loc_5 / _loc_4;
                _loc_3 = relativeList[_loc_5] as RelativeVideo;
                if (_loc_2 == param1)
                {
                    _loc_3.visible = true;
                }
                else
                {
                    _loc_3.visible = false;
                }
                _loc_5++;
            }
            prevNext.start();
            return;
        }// end function

        public function goTransform(param1:uint, param2:uint, param3:Boolean) : void
        {
            var _loc_6:RelativeVideo = null;
            if (relativeList == null)
            {
                return;
            }
            totalPage = Math.ceil(relativeList.length / (videoXNum * videoYNum));
            prevNext.setLength(totalPage);
            prevNext.setIndex(0);
            var _loc_4:* = NewSkinConsts.RELATIVE_ITEM_WIDTH;
            var _loc_5:* = NewSkinConsts.RELATIVE_ITEM_HEIGTH;
            param1 = xBetween * (videoXNum - 1) + _loc_4 * videoXNum;
            showTitle.x = 0;
            showTitle.y = 0;
            prevNext.x = param1 - prevNext.width;
            prevNext.y = 0;
            var _loc_7:* = new Point(0, showTitle.y + showTitle.height + 10);
            var _loc_8:* = new Point(0, showTitle.y + showTitle.height + 10).x;
            var _loc_9:* = _loc_7.y;
            var _loc_10:int = 0;
            while (_loc_10 < relativeList.length)
            {
                
                _loc_6 = relativeList[_loc_10] as RelativeVideo;
                _loc_6.x = _loc_8;
                _loc_6.y = _loc_9;
                _loc_8 = _loc_8 + (xBetween + _loc_4);
                if ((_loc_10 + 1) % videoXNum == 0)
                {
                    _loc_8 = _loc_7.x;
                    _loc_9 = _loc_9 + (yBetween + _loc_5);
                    if ((_loc_10 + 1) % (videoXNum * videoYNum) == 0)
                    {
                        _loc_9 = _loc_7.y;
                    }
                }
                _loc_10++;
            }
            goPage(prevNext.getIndex());
            graphics.clear();
            RectDisplayUtil.drawRect(graphics, param1, height, 52428, 0);
            return;
        }// end function

        public function setXYBetween(param1:Number, param2:Number) : void
        {
            xBetween = param1;
            yBetween = param2;
            return;
        }// end function

        public function startTransFinishTimeout() : void
        {
            return;
        }// end function

        public function stopTransFinishTimeout() : void
        {
            return;
        }// end function

    }
}
